class UsersController < ApplicationController

  def login
    return if generate_blank
    if @user = User.authenticate(params[:user][:login], params[:user][:password])
      session[:user] = @user
      flash[:notice] = 'Login successful'
      redirect_to_stored_or_default :action => 'home'
    else
      @login = params[:user][:login]
      flash.now[:warning] = 'Login unsuccessful'
    end
  end

  def home
    render(:layout => false)
  end

  def signup
    return if generate_blank
    @user = User.new(params[:user])
    if @user.save
      redirect_to :action => "login"
    end
  end

  def logout
    session[:user] = nil
    redirect_to :action => "login"
  end

  def show_all
    show_content(true)
  end

  def show_new
    show_content(false)
  end


  private

  def show_again
    if params[:is_category]
      redirect_to(:controller => 'categories', :action => 'show',
                  :id => params[:show_id])
    else
      redirect_to(:controller => 'subscriptions', :action => 'show',
                  :id => params[:show_id])
    end
  end

  def show_content(all)
    user.update_attribute(:display_all, all)
    user.reload
    show_again
  end

end
